package az.mecid.rssreader.client.presenter;

import az.mecid.rssreader.client.event.FeedAddedEvent;
import az.mecid.rssreader.client.event.FeedAddedHandler;
import az.mecid.rssreader.client.event.FeedAddingFailedEvent;
import az.mecid.rssreader.client.model.FeedItem;
import az.mecid.rssreader.client.services.FeedService;
import az.mecid.rssreader.client.services.SubscribeServiceAsync;
import az.mecid.rssreader.client.services.utils.Callback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: mega
 * Date: 02.03.2010
 * Time: 2:14:38
 */
public class ReaderPresenter implements Presenter, ValueChangeHandler<String> {

    public interface Display{
        HasClickHandlers getAddButton();
        HasSelectionHandlers getMenu();
        HasValue<String> getAddedFeed();
        void setData(List<FeedItem> items);
        Widget asWidget();
    }

    private Display display;
    private HandlerManager eventBus;
    private SubscribeServiceAsync feedServiceAsync;

    public ReaderPresenter(Display display, HandlerManager eventBus, SubscribeServiceAsync feedServiceAsync) {
        this.display = display;
        this.eventBus = eventBus;
        this.feedServiceAsync = feedServiceAsync;
    }

    public void go(HasWidgets container) {
        bind();
        container.clear();
        container.add(display.asWidget());
        fetchContent();
    }

    private void fetchContent() {
        FeedService.App.getInstance().getFeedItems(new Callback<List<FeedItem>>() {
            @Override
            public void done(List<FeedItem> result) {
                display.setData(result);
            }
        });
    }

    private void bind() {
        History.addValueChangeHandler(this);

        display.getAddButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                subscribeFeed();
            }
        });

        eventBus.addHandler(FeedAddedEvent.TYPE,new FeedAddedHandler() {
            public void onFeedAdded(FeedAddedEvent event) {
//                fetchContent();
            }
        });
    }

    private void subscribeFeed() {
        feedServiceAsync.subscribeFeed(display.getAddedFeed().getValue(),new Callback<Boolean>() {
            @Override
            public void done(Boolean result) {
                GwtEvent event;
                if(result)
                    event = new FeedAddedEvent();
                else
                    event = new FeedAddingFailedEvent();
                eventBus.fireEvent(event);
            }
        });

        display.getAddedFeed().setValue("",false);
    }

    public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {

    }
}
